package com.qf.dao.Impl;


import com.qf.bean.AddressBean;
import com.qf.dao.AddressesDao;
import com.qf.pojo.Address;
import com.qf.util.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class AddressesDaoImpl implements AddressesDao {

    private QueryRunner queryRunner = new QueryRunner();

    @Override
    public List<AddressBean> findAll(Integer uid) {
        //获取连接
        Connection connection = DBUtils.getConnection();

        //sql
        String sql = "SELECT aid,tag,name,province_name AS province," +
                "city_name city,area_name area,address,phone FROM t_address where uid = ?";

        //调用方法
        List<AddressBean> addressBeanList = null;

        try {
            addressBeanList = queryRunner.query(connection, sql, new BeanListHandler<>(AddressBean.class),uid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return addressBeanList;
    }

    @Override
    public Address findById(Integer aid) {
        //获取连接
        Connection connection = DBUtils.getConnection();

        //sql
        String sql = "SELECT * FROM t_address where aid = ?";

        //调用方法
        Address address = null;

        try {
            address = queryRunner.query(connection, sql, new BeanHandler<>(Address.class),aid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return address;
    }
}
